<?php

class Application_Model_DbTable_User extends Zend_Db_Table_Abstract {

    protected $_name = 'user';
    protected $_primary = "id_user"; //clé primaire
    protected $_sequence = true; // auto incrementé

    public function getAllUser() {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('u' => 'user'), array('id_user', 'titre', 'nom', 'prenom'))
                ->joinleft(array('a' => 'adresse'), ' u.id_adresse=a.id_adresse', array('adresse', 'ville', 'cpostal'))
                ->order('u.nom');
        return $this->fetchAll($select);
    }

    public function getUser($id_user) {

        // la verification de l'existence de id est faite dans le controller
        $id_user = $this->_db->quote($id_user, 'INTEGER');

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('u' => 'user'), array(
                    'id_user', 'id_cabinet', 'titre', 'nom', 'prenom', 'email', 'login', 'role', 'password'))
                ->joinleft(array('a' => 'adresse'), ' u.id_adresse=a.id_adresse') // array(champs) pour resteindre les champs
                ->joinLeft(array('c' => 'cabinet'), ' u.id_cabinet=c.id_cabinet', array('nom as nom_cabinet'))
                ->where('u.id_user = ?', $id_user);

        //echo $select;
        $row = $this->fetchRow($select);

        if (!$row) {
            throw new Exception("Utilisateur nr $id est introuvable.");
        }
        return $row->toArray();
    }

    public function addUser($data) {

        $user = Cab_Utils::setUcfist($data['User'], array('titre', 'id_user', 'email', 'id_cabinet', 'login', 'password', 'role'));
        $adresse = Cab_Utils::setUcfist($data['Adresse'], array('cpostal', 'portable', 'tel'));

        $user['id_user'] = null;
        $adresse['id_adresse'] = null;

        $date = new Zend_Date();
        $user['date_user'] = $date->toString('yyyy-MM-dd');

        $adresseTable = new Application_Model_DbTable_Adresse();
        $id_adresse = $adresseTable->addAdresse($adresse);

        if (isset($id_adresse) && $id_adresse > 0) {
            $user['id_adresse'] = $id_adresse;
            $this->insert($user);
            return $user;
            
        } else {
            throw new Exception("Erreur d'insertion de l'adresse.");
        }
    }

    public function editUser($data) {

        // echo "<h2>editUser</h2>";
        // premiere lettre des mots en majuscule, champs a ne pas traiter
        $user = Cab_Utils::setUcfist($data['User'], array('titre', 'id_user', 'email', 'id_cabinet', 'login', 'password', 'role'));
        $adresse = Cab_Utils::setUcfist($data['Adresse'], array('cpostal', 'portable', 'tel'));

        $adresseTable = new Application_Model_DbTable_Adresse();
        $adresseTable->editAdresse($adresse, (int) $adresse['id_adresse']);

        $auth = Zend_Auth::getInstance();

        if (isset($auth) && $user['role'] != $auth->getStorage()->read()->role) {
            $auth->getStorage()->read()->role = $user['role'];
        }

        $this->update($user, 'id_user = ' . (int) $user['id_user']);
    }

    public function changePassword($data) {
        unset($data['password_bis']);
        $this->update($data, 'id_user = ' . (int) $data['id_user']);
    }

    public function deleteUser($id) {
        $this->delete('id_user =' . (int) $id);
    }

}

